package com.mzba.place.fragment;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;
import com.mzba.place.PlaceApplication;
import com.mzba.place.R;
import com.mzba.place.adapter.MyArrayAdapter;
import com.mzba.place.adapter.ReportListAdapter;
import com.mzba.place.entity.ReportEntity;
import com.mzba.place.ui.MainActivity;
import com.mzba.place.ui.ReportDetailActivity;
import com.mzba.place.utils.ImageLoader;
import com.mzba.place.utils.Utils;

/**
 * 
 * @author 06peng
 * 
 */
public class ReportListFragment extends SherlockFragment {

	public FrameLayout mainLayout;
	private ListView listView;

	private List<ReportEntity> reports;
	private MainActivity mainActivity;
	private ReportListAdapter reportListAdapter;
	private double localLat;
	private double localLon;
	ImageLoader mImageLoader;
	PlaceApplication app;
	
	public View filterView;
	public boolean firstReportType;
	public boolean firstReportTime;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mainActivity = (MainActivity) getSherlockActivity();
		app = (PlaceApplication) mainActivity.getApplication();
		localLat = app.latitude;
		localLon =  app.longitude;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mainLayout = (FrameLayout) inflater.inflate(R.layout.report_list_layout, null);
		return mainLayout;
	}
	
	@Override
	public void onResume() {
		init();
		super.onResume();
	}

	public void init() {
		if (mainActivity == null) {
			return;
		}
		if (mainActivity.isLoading) {
			mainActivity.menu.clear();
			mainActivity.setSupportProgressBarIndeterminateVisibility(true);
			return;
		}
		initFilterView();
        initFilterTips();
		mainActivity.setSupportProgressBarIndeterminateVisibility(false);
		reports = mainActivity.getReports();
		mImageLoader = ImageLoader.getInstance(getActivity());
		listView = (ListView) mainLayout.findViewById(R.id.list_report);
		reportListAdapter = new ReportListAdapter(getActivity(), reports, localLat, localLon, mImageLoader);
		listView.setAdapter(reportListAdapter);
		
		listView.setOnScrollListener(onScrollListener);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				Intent intent = new Intent();
				intent.setClass(getActivity(), ReportDetailActivity.class);
				// 此arg2 从1开始。。
				intent.putExtra("report_detail", reportListAdapter.getReportList().get(arg2-1));
				startActivity(intent);
			}
		});
	}

	OnScrollListener onScrollListener = new OnScrollListener() {
		@Override
		public void onScrollStateChanged(AbsListView view, int scrollState) {
			switch (scrollState) {
			case OnScrollListener.SCROLL_STATE_FLING:
				mImageLoader.lock();
				break;
			case OnScrollListener.SCROLL_STATE_IDLE:
				mImageLoader.unlock();
				break;
			case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
				mImageLoader.lock();
				break;
			default:
				break;
			}
		}

		@Override
		public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
			
		}
	};

	public void initFilterView() {
		try {
			firstReportType = false;
			firstReportTime = false;
			if (mainLayout != null) {
				filterView = mainLayout.findViewById(R.id.filter_layout);
				filterView.setVisibility(View.GONE);
				Spinner reportTypeSpinner = (Spinner) mainLayout.findViewById(R.id.report_type_spinner);
				Spinner reportTimeSpinner = (Spinner) mainLayout.findViewById(R.id.report_time_spinner);
				MyArrayAdapter reportTypeAdapter = new MyArrayAdapter(mainActivity, R.layout.spinner_item);
				reportTypeAdapter.addAll(getResources().getStringArray(R.array.reportType));
				reportTypeAdapter.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
				reportTypeSpinner.setAdapter(reportTypeAdapter);
				
				MyArrayAdapter reportTimeAdapter = new MyArrayAdapter(mainActivity, R.layout.spinner_item);
				reportTimeAdapter.addAll(getResources().getStringArray(R.array.reportTime));
				reportTimeAdapter.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
				reportTimeSpinner.setAdapter(reportTimeAdapter);
				reportTypeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

					@Override
					public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
						mainActivity.report_type = arg2;
						if (firstReportType) {
							filterView.setVisibility(View.GONE);
							if (mainActivity.mapFragment != null) {
								mainActivity.mapFragment.filterView.setVisibility(View.GONE);
								mainActivity.mapFragment.initFilterTips();
							}
							initFilterTips();
							mainActivity.load();
						}
						firstReportType = true;
					}

					@Override
					public void onNothingSelected(AdapterView<?> arg0) {
						
					}
				});
				reportTimeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

					@Override
					public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
						mainActivity.report_time = arg2;
						if (firstReportTime) {
							filterView.setVisibility(View.GONE);
							if (mainActivity.mapFragment != null) {
								mainActivity.mapFragment.filterView.setVisibility(View.GONE);
								mainActivity.mapFragment.initFilterTips();
							}
							initFilterTips();
							mainActivity.load();
						}
						firstReportTime = true;
					}

					@Override
					public void onNothingSelected(AdapterView<?> arg0) {
						
					}
				});
				reportTypeSpinner.setSelection(mainActivity.report_type);
				reportTypeSpinner.setSelected(true);
				reportTimeSpinner.setSelection(mainActivity.report_time);
				reportTimeSpinner.setSelected(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void initFilterTips() {
		View filterTipsView = mainLayout.findViewById(R.id.home_filter_tips);
		if (mainActivity.report_time == 0 && mainActivity.report_type == 0) {
			filterTipsView.setVisibility(View.GONE);
		} else {
			TextView tv_type = (TextView) filterTipsView.findViewById(R.id.filter_tv);
			tv_type.setText(Utils.getReportTime(mainActivity)[mainActivity.report_time] + "发生的" +
		               Utils.getReportType(mainActivity)[mainActivity.report_type] + "事件");
			filterTipsView.setVisibility(View.VISIBLE);
		}
	}
}
